package com.hiv.mapper;

import com.hiv.common.param.page.PageQueryBatchParam;
import com.hiv.mapper.domain.Batch;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 批次Mapper接口
 *
 * @author zhanghong
 * @date 2022-09-27
 */
@Mapper
@Repository
public interface BatchMapper {
    Batch selectById(Long id);
    List<Batch> selectList(Batch batch);
    int insert(Batch batch);
    int batchInsert(List<Batch> batchList);
    int update(Batch batch);
    int deleteById(Long id);
    int deleteByIds(String[] ids);

    @Select({"<script>",
            "   select count(1) from batch",
            "   <where>",
            "       deleted=0",
            "       <if test=\"param.code\">and code like concat('%', #{param.code}, '%')</if>",
            "       <if test=\"param.express\">and express like concat('%', #{param.express}, '%')</if>",
            "       <if test=\"param.hospitalId\">and hospital_id=#{param.hospitalId}</if>",
            "       <if test=\"param.inspectionId\">and inspection_id=#{param.inspectionId}</if>",
            "       <if test=\"param.hospitalIdList != null and param.hospitalIdList.size() != 0\">",
            "           and hospital_id in",
            "           <foreach collection=\"param.hospitalIdList\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "               #{item} ",
            "           </foreach> ",
            "       </if> ",
            "   </where>",
            "</script>"})
    long pageCount(@Param("param") PageQueryBatchParam param);
    @Select({"<script>",
            "   select * from batch",
            "   <where>",
            "       deleted=0",
            "       <if test=\"param.code\">and code like concat('%', #{param.code}, '%')</if>",
            "       <if test=\"param.express\">and express like concat('%', #{param.express}, '%')</if>",
            "       <if test=\"param.hospitalId\">and hospital_id=#{param.hospitalId}</if>",
            "       <if test=\"param.inspectionId\">and inspection_id=#{param.inspectionId}</if>",
            "       <if test=\"param.hospitalIdList != null and param.hospitalIdList.size() != 0\">",
            "           and hospital_id in",
            "           <foreach collection=\"param.hospitalIdList\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "               #{item} ",
            "           </foreach> ",
            "       </if> ",
            "   </where>",
            "   order by create_time desc limit #{limit} offset #{offset}",
            "</script>"})
    List<Batch> pageQuery(@Param("param") PageQueryBatchParam param, @Param("limit") int limit, @Param("offset") long offset);

    @Select({"<script>",
            "select b.* from batch b ",
            "inner join sampling_box sb on sb.batch_id=b.id",
            "inner join sampling_box_record sbr on sbr.box_id=sb.id",
            "where sbr.sampling_id=#{samplingId}",
            "limit 1",
            "</script>"})
    Batch queryBySamplingRecord(Long samplingId);

    Batch queryByExpress(String express);
}
